window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');

document.addEventListener('DOMContentLoaded', function () {

    // 导航栏滚动效果
    window.addEventListener('scroll', function () {
        const navbar = document.querySelector('.navbar');
        const backToTop = document.getElementById('backToTop');

        if (window.scrollY > 50) {
            navbar.classList.add('scrolled');
        } else {
            navbar.classList.remove('scrolled');
        }

        if (window.scrollY > 300) {
            backToTop.classList.add('active');
        } else {
            backToTop.classList.remove('active');
        }
    });

    // 移动端菜单切换
    const menuToggle = document.getElementById('mobileMenu');
    const navLinks = document.querySelector('.nav-links');

    menuToggle.addEventListener('click', function () {
        menuToggle.classList.toggle('active');
        navLinks.classList.toggle('active');
    });

    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();

            if (this.getAttribute('href') === '#') return;

            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                window.scrollTo({
                    top: target.offsetTop - 80,
                    behavior: 'smooth'
                });

                // 关闭移动菜单
                menuToggle.classList.remove('active');
                navLinks.classList.remove('active');
            }
        });
    });

    // 返回顶部按钮
    document.getElementById('backToTop').addEventListener('click', function () {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });


    // 仓鼠头像动画
    const hamsterAvatar = document.getElementById('hamsterAvatar');
    if (hamsterAvatar) {
        hamsterAvatar.addEventListener('mouseenter', function () {
            this.style.animationPlayState = 'paused';
        });

        hamsterAvatar.addEventListener('mouseleave', function () {
            this.style.animationPlayState = 'running';
        });
    }

    // 联系表单提交
    const contactForm = document.getElementById('contactForm');
    if (contactForm) {
        contactForm.addEventListener('submit', function (e) {
            e.preventDefault();

            // 这里可以添加表单验证和AJAX提交逻辑
            alert('感谢您的留言！我会尽快回复您。');
            this.reset();
        });
    }

    // 打字动画效果
    const typingElement = document.querySelector('.animate-typing');
    if (typingElement) {
        const text = typingElement.textContent;
        typingElement.textContent = '';

        let i = 0;
        const typingInterval = setInterval(function () {
            if (i < text.length) {
                typingElement.textContent += text.charAt(i);
                i++;
            } else {
                clearInterval(typingInterval);
                typingElement.classList.remove('animate-typing');
            }
        }, 100);
    }
});

// Intersection Observer 用于动画触发
function initIntersectionObserver() {
    const observerOptions = {
        root: null,
        rootMargin: '0px',
        threshold: 0.1
    };

    const observer = new IntersectionObserver(function (entries, observer) {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('animated');
                observer.unobserve(entry.target);
            }
        });
    }, observerOptions);

    // 观察需要动画的元素
    document.querySelectorAll('.fade-in, .slide-up').forEach(element => {
        observer.observe(element);
    });
}

// 初始化Intersection Observer
if ('IntersectionObserver' in window) {
    initIntersectionObserver();
}

function updateImg() {
    // 创建一个新的Image对象
    let img = new Image();
    // 设置图片的src属性
    img.src = "https://pic.imgdb.cn/item/65ddd7409f345e8d0394f256.webp";
    // 监听图片加载完成事件
    $(img).on("load", function () {
        // 修改body的背景样式为加载完成的图片
        $(".hero-image").addClass("clear-after");
        //$(".hero-image").attr("src", img.src);
        $(".hero-image").css("background-image", "url('" + img.src + "')");
    });
}
updateImg()